/* Copyright (c) 2004-2024 peigen.info. All rights reserved. */

package info.peigen.hotpot.manage.core.service.processor.create;

import com.google.common.collect.Lists;
import info.peigen.hotpot.common.core.util.PasswordUtils;
import info.peigen.hotpot.common.service.base.processor.Processor;
import info.peigen.hotpot.common.service.base.processor.ProcessorServiceContext;
import info.peigen.hotpot.manage.core.domain.Organization;
import info.peigen.hotpot.manage.core.domain.Position;
import info.peigen.hotpot.manage.core.domain.Role;
import info.peigen.hotpot.manage.core.domain.User;
import info.peigen.hotpot.manage.core.service.order.UserCreateOrder;
import info.peigen.hotpot.manage.core.service.processor.AbstractHotpotManageServiceProcessor;
import info.peigen.hotpot.manage.core.util.convertor.UserConvertor;
import org.noear.solon.annotation.Component;

/**
 * <b>(UserCreateProcessor)</b>
 * "用户" 创建处理器
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2024-06-28
 */
@SuppressWarnings({"rawtypes"})
@Component
public class UserCreateProcessor extends AbstractHotpotManageServiceProcessor implements Processor {
    @Override
    public Integer level() {
        return 25;
    }

    @Override
    public void execute(ProcessorServiceContext serviceContext) {
        UserCreateOrder order  = (UserCreateOrder) serviceContext.getOrder();
        User            domain = UserConvertor.INSTANCE.convertToDomain(order);
        domain.setOrganizations(Lists.newArrayList(Organization.defaultOrganization()))
                .setPositions(Lists.newArrayList(Position.defaultPosition()))
                .setRoles(Lists.newArrayList(Role.defaultRole()));

        // 密码处理
        String password = PasswordUtils.generatePwdEncrypt(domain.getPassword(), domain.getSalt(), domain.getCreateTime());
        domain.setPassword(password);
        serviceContext.setDomain(domain);
    }

}